//
//  FoodEditViewController.swift
//  Food
//
//  Created by 罗林轩 on 2023/12/16.
//

import UIKit
import CoreData

class FoodEditViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var segmentController: UISegmentedControl!
    var breakfirst : [String] = []
    var lunch : [String] = []
    var dinner : [String] = []
    var type : Int = 0
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if (self.type == 0) {
            return breakfirst.count
        } else if (self.type == 1) {
            return lunch.count
        } else {
            return dinner.count
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell : UITableViewCell = tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(UITableViewCell.classForCoder()), for: indexPath)
        var dataArray : [String] = getData()
        cell.textLabel?.text = dataArray[indexPath.item]
        return cell
    }
    
    
    /// 处理数据
    /// - Parameter string: 参数
    func processingData(_ string : String) {
        if (self.type == 0) {
            breakfirst.append(string)
        } else if (self.type == 1) {
            lunch.append(string)
        } else {
            dinner.append(string)
        }
    }
    
    
    /// 获取数据
    /// - Returns: 返回数据
    func getData() -> [String] {
        if (self.type == 0) {
            return breakfirst
        } else if (self.type == 1) {
            return lunch
        } else {
            return dinner
        }
    }
    

    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: NSStringFromClass(UITableViewCell.classForCoder()))
        self.segmentController.addTarget(self, action: #selector(change(_:)), for: .valueChanged)
    }
    
    @objc func change(_ sender:UISegmentedControl){
        self.type = sender.selectedSegmentIndex
        self.tableView.reloadData()
    }
    
    @IBAction func save() {
        Tool().saveData(breakfirst: breakfirst, lunch: lunch, dinner: dinner)
    }
    
    @IBAction func add() {
        let alert :UIAlertController = UIAlertController(title: "吃的什么?", message: "请输入内容", preferredStyle: .alert)
        let makeSure = UIAlertAction(title: "确定", style: .default) { act in
            if let text = alert.textFields?.first?.text {
                self.processingData(text)
                self.tableView.reloadData()
            }
        }
        
        let cancel = UIAlertAction(title: " 取消", style: .default) { act in
            
        }
        alert.addAction(makeSure)
        alert.addAction(cancel)
        alert.addTextField { tf in
            
        }
        
        self.present(alert, animated: true)
    }
    
  

}
